package com.timothy.android;

import java.util.ArrayList;
import java.util.HashMap;

import com.timothy.answer.BaseStationSelection;
import com.timothy.service.MessageSender;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class BaseStations extends BaseActivity {
	private final String LOG_TAG = "BaseStations";
	private ListView mylist = null;
	private EditText baseID = null;
	private EditText loginId = null;
	private EditText loginPwd = null;
	
	private Button refreshBtn = null;
	private Button connectBtn = null;
	
	private String pcIP;
	private int pcPort;
	
	ArrayList<HashMap<String, Object>> listItem;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.basestations);
		//set screen orientation to portrait
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		mylist = (ListView) findViewById(R.id.myListView);
		baseID = (EditText) findViewById(R.id.baseIdTxt);
		baseID.clearFocus();
		
		loginId = (EditText) findViewById(R.id.loginIdTxt);
		loginPwd = (EditText) findViewById(R.id.loginPwdTxt);
		
		refreshBtn = (Button) findViewById(R.id.refreshBtn);
		connectBtn = (Button) findViewById(R.id.connectBtn);
		
        //生成动态数组，加入数据
        listItem = new ArrayList<HashMap<String, Object>>();
        
        for(int i=0;i<10;i++){
        	HashMap<String, Object> map = new HashMap<String, Object>();
//        	map.put("ItemImage", R.drawable.checked);//图像资源的ID
        	map.put("hostName", "host"+i);
        	map.put("port", 9990+i);
        	map.put("hostAddr", "192.168.1.102");
        	listItem.add(map);
        }
		
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源 
                R.layout.stations_list,//ListItem的XML实现
                //动态数组与ImageItem对应的子项        
                new String[] {"hostName","port","hostAddr"},
                //ImageItem的XML文件里面的一个ImageView,两个TextView ID
        		new int[] {R.id.hostNameTV,R.id.portTV,R.id.hostAddrTV}
        );
        mylist.setAdapter(listItemAdapter);
        mylist.setOnItemClickListener(new OnItemClickListener(){
        	@Override
			public void onItemClick(AdapterView<?> adapter, View view, int arg2,
					long arg3) {
        		
        		for(int i=0;i<adapter.getChildCount();i++){
        			adapter.getChildAt(i).setBackgroundColor(Color.parseColor("#ffaaff"));
        		}
        		
        		TextView hostName = (TextView) view.findViewById(R.id.hostNameTV); 
        		TextView port = (TextView) view.findViewById(R.id.portTV); 
        		TextView hostAddr = (TextView) view.findViewById(R.id.hostAddrTV);
        		
        		view.setBackgroundColor(Color.parseColor("#8290d8"));
				baseID.setText(hostName.getText().toString());
				
				pcIP = hostAddr.getText().toString();
				pcPort = Integer.valueOf(port.getText().toString());
				
				Log.i(LOG_TAG,"Selected Base Station IP:"+pcIP);
				Log.i(LOG_TAG,"Selected Base Station port:"+pcPort);
			}      	
        });
        
        //login into baseStation
        connectBtn = (Button) this.findViewById(R.id.connectBtn);
        connectBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
//				MessageSender.PC_IP =  pcIP;
//				MessageSender.PC_PORT = pcPort;
				
				Bundle bundle = new Bundle();
				bundle.putString("baseStationName", baseID.getText().toString());
//				bundle.putString("hostAddr", pcIP);
//				bundle.putString("port", String.valueOf(pcPort));
				
				bundle.putString("loginId", loginId.getText().toString());
				bundle.putString("loginPwd", loginPwd.getText().toString());
			    
				//set baseStation information
				BaseStationSelection bss = new BaseStationSelection();
				bss.setBaseStationName(baseID.getText().toString());
				bss.setUserName(loginId.getText().toString());
				bss.setPassword(loginPwd.getText().toString());
				
				//Send Phone Information to PC
//				if( MessageSender.sendBaseStationSelection(bss) ) {
				if(bss.getBaseStationName()==null||"".equals(bss.getBaseStationName())){
					ShowDialog("Base Station Name can not be empty!");
				}else{
					Intent intent = new Intent();
				    intent.setClass(BaseStations.this, HomeActivity.class);
				    intent.putExtras(bundle);
				    startActivity(intent);					
				}

//				} else {
//					ShowDialog("Can not Connect,please try again!");
//				}
			}			
		});
        
	}
	
	public void ShowDialog(String msg) {
		new AlertDialog.Builder(this).setTitle("提示")
		                             .setMessage(msg)
				                     .setPositiveButton("OK", new DialogInterface.OnClickListener() {
										                    	  public void onClick(DialogInterface dialog, int which) {
//										                    		  hostAddr.performClick();
										                    	  }
										                      })
				                     .show();
	}
	
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		setTitle("You Clicked "+item.getItemId()+""); 
		return super.onContextItemSelected(item);
	}
	
}